<template>
  <div class="follow-main">
    <div class="follow-header follow-header-info">
      <div class="breadcrumb flex-c-s">
        <p class="item cur">我的投稿</p>
      </div>
    </div>
    <div class="follow-content section">
      <div class="content" style="padding-bottom: 10px">
        <ul class="relation-list">
          <li
            class="list-item clearfix relative"
            v-for="post in postList"
            :key="post.uid"
          >
            <div class="cover-container">
              <a :href="`/community?postId=${post.id}`">
                <img
                  class="fit-cover"
                  v-if="post.cover"
                  :src="post.cover.split(',')[0]"
                />
              </a>
            </div>
            <div class="content flex col">
              <a :href="`/community?postId=${post.id}`" class="title">
                <span>{{ post.postTitle }}</span>
              </a>
              <p class="desc text-ellipsis">
                {{ post.postContent }}
              </p>
              <div class="publish-time">
                <span>{{ parseDateArrayToDateString(post.createTime) }}</span>
              </div>
            </div>
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref } from "vue";
import { getUserPostList } from "@/api/post";
import { parseDateArrayToDateString } from "@/utils/common";

const postList = ref<Community[]>([]);

onMounted(async () => {
  const result = await getUserPostList();
  console.log(result.data);
  if (result.data.code === 200) {
    postList.value = result.data.data;
    postList.value = postList.value.sort((a, b) => {
      return (
        //@ts-ignore
        new Date(...b.createTime).getTime() -
        //@ts-ignore
        new Date(...a.createTime).getTime()
      );
    });
  }
});
</script>

<style scoped lang="scss">
.list-item {
  position: relative;
  height: fit-content;
  display: flex;
  align-items: start !important;

  .content {
    width: 60%;
    position: relative;

    .title {
      font-size: 22px;
      font-weight: bold;
      margin-bottom: 1rem;
    }

    .desc {
      margin-bottom: 1rem;
    }
  }

  .action {
    position: absolute;
    right: 0;
    top: 45%;
  }
}

.cover-container {
  height: 150px;
  width: 20%;
  margin-right: 20px;

  a {
    height: 100%;
    width: 100%;
    display: block;
  }
}
</style>
